package panda.client.ui.widgets;

import java.util.ArrayList;
import java.util.List;

import panda.client.enumerations.ZIndexLevel;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HTML;

/**
 * Singleton for the main tooltip.
 * @author zane.tian@gmail.com
 */
public class Tooltip extends HTML {
	private static Tooltip tooltip = new Tooltip("");
	
	private Tooltip(String text) {
		super(text);
		setWordWrap(false);
		// addStyleName("card-tooltip");
		
		DOM.setStyleAttribute(getElement(), "fontSize", "9pt");
//		DOM.setStyleAttribute(getElement(), "backgroundImage", "url(notetip-bg.gif)");
//		DOM.setStyleAttribute(getElement(), "backgroundRepeat", "repeat-x");
//		DOM.setStyleAttribute(getElement(), "border", "1px solid rgb(140, 190, 250)");
//		DOM.setStyleAttribute(getElement(), "color", "black");
		DOM.setStyleAttribute(getElement(), "opacity", "0.90");
		DOM.setStyleAttribute(getElement(), "filter", "alpha(opacity=90)");
//		DOM.setStyleAttribute(getElement(), "padding", "0px 5px 0px 5px");
		DOM.setStyleAttribute(getElement(), "zIndex", String.valueOf(ZIndexLevel.TOOLTIP.getZIndex()));
		
		NoteTip.setShadedStyle(getElement());
		DOM.setStyleAttribute(getElement(), "border", "1px solid black");
	}
	
	public static Tooltip get() {
		return tooltip;
	}
	
	public void hide() {
		this.setVisible(false);
	}
	
	/**
	 * Fades out tooltip over a short time interval.  Assumes tooltip has 80% opacity max.
	 * @param milliseconds
	 */
	public void fadeOutTooltip(int milliseconds) {
		final int timeSegment = milliseconds / 8;
		
		final List<Timer> timers = new ArrayList<Timer>();
		for(int i = 0; i < 8; i++) {
			final int index = i;
			timers.add(new Timer() {
				public void run() {
					DOM.setStyleAttribute(tooltip.getElement(), "opacity", "0." + index*10);
					DOM.setStyleAttribute(tooltip.getElement(), "filter", "alpha(opacity=" + index*10 + ")");
					
					if(index != 0) { // each timer schedules a lighter opacity timer
						timers.get(index - 1).schedule(timeSegment);
					}
				}
			});
		}
		
		timers.get(7).schedule(timeSegment); // schedule the most opaque timer to start the chain
	}
	
	/**
	 * Fades in tooltip over a short time interval.  Assumes tooltip has 80% opacity max.
	 * @param milliseconds
	 */
	public void fadeInTooltip(int milliseconds) {
		final int timeSegment = milliseconds / 8;
		
		final List<Timer> timers = new ArrayList<Timer>();
		for(int i = 7; i >=0; i--) {
			final int index = i;
			final int reverseIndex = 7-i;
			timers.add(new Timer() {
				public void run() {
					DOM.setStyleAttribute(tooltip.getElement(), "opacity", "0." + index*10);
					DOM.setStyleAttribute(tooltip.getElement(), "filter", "alpha(opacity=" + index*10 + ")");
					
					if(reverseIndex != 0) { // each timer schedules a darker opacity timer
						timers.get(reverseIndex - 1).schedule(timeSegment);
					}
				}
			});
		}
		
		timers.get(7).schedule(timeSegment); // schedule the least opaque timer
	}
}